import DB from "@/sqlite/DB";

/**添加或者更新加班费，主键是日期
 * @param {Object} submitParam 单个日期的加班信息
 * @param {Number} submitParam.money 加班费
 * @param {String} submitParam.notes 备注
 * @param {Date} submitParam.date 加班日期
 * @param {String} submitParam.info 日历日期下面显示内容
 */
async function recordSalary(submitParam) {
  try {
    // 新增 DB.insertTableData(表名, 对应表头列的数据)
    let sql = `${submitParam.money},'${submitParam.notes}','${submitParam.date}','${submitParam.info}'`;
    let condition = "'money','notes','date','info'";
    await DB.insertOrReplaceData(DB.recordsTable, sql, condition);
    return '添加/更新加班费成功';
  } catch (e) {
    throw e;
  }
}

/**通过年月查询加班费记录
 * @param {String} month 日期
 * @example queryRecordsByMonth('2023-12')
 */
async function queryRecordsByMonth(month) {
  try {
    const searchCondition = `WHERE strftime('%Y-%m', date) = '${month}'`
    const records = await DB.selectTableData(DB.recordsTable, searchCondition);
    return records;
  } catch (e) {
    throw e;
  }
}

/**通过具体日期查询加班费记录
 * @param {String} day 日期
 * @example queryRecordsByMonth('2023-12-20')
 */
async function queryRecordsByDay(day) {
  try {
    const searchCondition = `WHERE date = '${day}'`
    const records = await DB.selectTableData(DB.recordsTable, searchCondition);
    return records;
  } catch (e) {
    throw e;
  }
}

/**通过具体日期删除该天的加班记录
 * @param {Object} day 日期
 * @example deleteRecordByDate('2023-12-20')
 */
async function deleteRecordByDate(day) {
  try {
    const searchCondition = `WHERE date = '${day}'`
    const result = await DB.deleteTableData(DB.recordsTable, searchCondition);
    return '删除记录成功';
  } catch (e) {
    throw e;
  }
}

export default {
  recordSalary,
  queryRecordsByMonth,
  queryRecordsByDay,
  deleteRecordByDate
}
